import 'package:flutter/material.dart';
import 'package:flutter_mall_app/pages/shopping_cart/cart_count.dart';
import '../../provide/shopping_cart.dart';
import 'package:provide/provide.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../model/CartInfoModel.dart';
import 'package:cached_network_image/cached_network_image.dart';

class CartItem extends StatelessWidget {
  final CartInfoModel item;

  const CartItem({Key key, this.item}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return
      Container(
      margin: EdgeInsets.all(4),
      height: ScreenUtil().setHeight(200),
      child: Card(
        elevation: 8,
        child: Row(
          children: <Widget>[
            _buildCartCheckBox(context,item),
            _buildCartImage(),
            _buildGoodsName(),
            _buildPrice(context,item),
          ],
        ),
      ),
    );
  }

  //列表item多选按钮
  Widget _buildCartCheckBox(context,item) {
    return Container(
      child: Checkbox(
        value: item.isCheck,
        activeColor: Colors.redAccent,
        onChanged: (value) {
          item.isCheck = value;
          Provide.value<ShoppingCartProvide>(context).changeCheckState(item);
        },
      ),
    );
  }

  //商品图片
  Widget _buildCartImage() {
    return Container(
      width: ScreenUtil().setWidth(150),
      padding: EdgeInsets.all(4),
      child: CachedNetworkImage(
        imageUrl: item.images,
      ),
    );
  }

//商品名称
  Widget _buildGoodsName() {
    return Expanded(
        flex: 4,
        child: Container(
          padding: EdgeInsets.all(8),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Text(
                "${item.goodsName}",
                style: TextStyle(fontSize: ScreenUtil().setSp(25)),
              ),
              CartCount(item: item,),
            ],
          ),
        ));
  }

//商品价格
  Widget _buildPrice(context,item) {
    return Expanded(
        flex: 2,
        child: Container(
          padding: EdgeInsets.all(8),
          alignment: Alignment.center,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text("￥${item.price}"),
              Container(
                child: IconButton(
                  iconSize: 25,
                  color: Colors.redAccent,
                  icon: Icon(Icons.delete),
                  onPressed: () {
                    Provide.value<ShoppingCartProvide>(context).deleteOneGoods(item.goodsId);
                  },
                ),
              ),
            ],
          ),
        ));
  }
}
